﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Entity.vo;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class PO : BasePage
    {
        public Guid POID
        {
            get
            {
                if (hiddenPOID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenPOID.Value);
            }
            set
            {
                hiddenPOID.Value = value.ToString();
           }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                
                Unit.Text = "只";
                if (Request.QueryString["POID"] != null)
                {
                    WsmPOEntity StockInEntity = new SystemBll().Get<WsmPOEntity>(new Guid(Request.QueryString["POID"]));
                    POID = StockInEntity.POID;
                    PODate.SelectedDate = StockInEntity.PODate;
                    POType.SelectedValue = StockInEntity.POType.ToString();
                    ddlPOFactory.SelectedValue = StockInEntity.POFactory.ToString();
                    txtPONO.Text = StockInEntity.PONO.ToString();
                    txtApplyMan.Text = StockInEntity.ApplyMan;
                    Memo.Text = StockInEntity.Memo;

                    DataTable dt = new StockBll().QueryPODetail(POID.ToString());
                    gridSubItems.DataSource = dt;
                    gridSubItems.DataBind();

                    if (StockInEntity.CheckStatus == "1")
                    {
                        link_saveBill.Visible = false;
                    }
                }
                else
                {
                    txtApplyMan.Text = base.CurrentUserEntity.UserName;
                }

                if (Request.QueryString["checkstatus"] != null)
                {
                    if (Request.QueryString["checkstatus"] == "0")
                    { ltnCancelcheck.Visible = false; ltncheck.Visible = true; link_saveBill.Visible = true; }
                    else { ltnCancelcheck.Visible = true; ltncheck.Visible = false; link_saveBill.Visible = false; }
                }
                else
                { ltnCancelcheck.Visible = false; ltncheck.Visible = false; }
            }
            
        }          
        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            DataTable dt = getDateFromGrid(-1);
            bool isExist = false;
            for (int index = 0; index < dt.Rows.Count;index++ )
            {
                String AccessoryId = dt.Rows[index]["AccessoryID"].ToString();
                if (AccessoryId == hiddenInstrumentID.Value.Trim()) {
                    dt.Rows[index]["Quantity"] = System.Convert.ToInt32(dt.Rows[index]["Quantity"]) + System.Convert.ToInt32(accessoryCount.Text.Trim());
                    isExist = true;
                }
            }
            if (!isExist)
            {
                DataRow dr = dt.NewRow();
                dr["AccessoryID"] = hiddenInstrumentID.Value.ToString();
                WsmAccessoryInfoEntity entity = new SystemBll().Get<WsmAccessoryInfoEntity>(new Guid(hiddenInstrumentID.Value));
                dr["AccessoryName"] = entity.AccessoryName;
                dr["AccessoryCompanyNumber"] = entity.CompanyInstrumentCode;
                dr["Unit"] = Unit.Text.Trim();
                dr["Quantity"] = accessoryCount.Text.Trim();
                dt.Rows.Add(dr);
            }
            gridSubItems.DataSource = dt;
            gridSubItems.DataBind();
        }

        private DataTable getDateFromGrid(int  i) {
            DataTable dt = new AccessoryInfo().getPOAccessoryInfoStructure();
            for (int index = 0; index < gridSubItems.Items.Count; index++)
            {   
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label AccessoryCompanyNumber = gridSubItems.Items[index].FindControl("AccessoryCompanyNumber") as Label;
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;
                Label Unit = gridSubItems.Items[index].FindControl("Unit") as Label;
                TextBox Quantity = gridSubItems.Items[index].FindControl("Quantity") as TextBox;
                Label POSID = gridSubItems.Items[index].FindControl("POSID") as Label;

                if (index != i)
                {
                    DataRow dr = dt.NewRow();                 
                    dr["AccessoryID"] = AccessoryID.Text;
                    dr["AccessoryCompanyNumber"] = AccessoryCompanyNumber.Text;
                    dr["AccessoryName"] = AccessoryName.Text;
                    dr["Unit"] = Unit.Text;
                    dr["Quantity"] = Quantity.Text;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        protected void link_saveBill_Click(object sender, EventArgs e)
        {
            if (gridSubItems.Items.Count < 1) { ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "alert('请输入采购明细！')", true); }
            WsmPOEntity stockInEntity;
            if (POID != Guid.Empty)
            {
                stockInEntity = new SystemBll().Get<WsmPOEntity>(POID);
                try
                {
                    new SystemBll().Delete<WsmPOEntity>(" and POID='" + POID + "'");
                    new SystemBll().Delete<WsmPOSEntity>(" and POID='" + POID + "'");
                }
                catch {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "alert('明细中已有配件发生变更,订购单更改失败')", true);
                    return;
                }
            }
            else
            {
                stockInEntity = new WsmPOEntity();
                stockInEntity.POID = Guid.NewGuid();
                stockInEntity.CreatedBy = CurrentUserID;
                stockInEntity.CreatedOn = DateTime.Now;
               
                stockInEntity.PODate = PODate.SelectedDate.Value;
            }
            stockInEntity.ApplyMan = txtApplyMan.Text.TrimEnd();
            stockInEntity.PONO = txtPONO.Text.TrimEnd();
            stockInEntity.Memo = Memo.Text;
            stockInEntity.POFactory = ddlPOFactory.SelectedValue;
            stockInEntity.POType = POType.SelectedValue;
            stockInEntity.Deleted = false;
            stockInEntity.CheckStatus = "0";
            new SystemBll().InsertOrUpdate(stockInEntity);
            for (int index = 0; index < gridSubItems.Items.Count; index++) {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label AccessoryCompanyNumber = gridSubItems.Items[index].FindControl("AccessoryCompanyNumber") as Label;
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;
                Label Unit = gridSubItems.Items[index].FindControl("Unit") as Label;
                TextBox Quantity = gridSubItems.Items[index].FindControl("Quantity") as TextBox;
                Label POSID = gridSubItems.Items[index].FindControl("POSID") as Label;

                WsmPOSEntity entity = new WsmPOSEntity();
                entity.POSID = Guid.NewGuid();
                entity.POID = stockInEntity.POID;
                entity.AccessoryID = new Guid(AccessoryID.Text);
                entity.AccessoryName = AccessoryName.Text;
                entity.Unit = Unit.Text;
                entity.AccessoryCompanyNumber = AccessoryCompanyNumber.Text;
                entity.Quantity = System.Convert.ToDecimal(Quantity.Text);
                entity.IsClose = "0";
                new SystemBll().InsertOrUpdate(entity);
            }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "alert('订购成功')", true);
        }
        protected void gridSubItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gridSubItems.DataSource = getDateFromGrid(e.Item.ItemIndex);
                gridSubItems.DataBind();
            }
        }
        protected void gridSubItems_ItemDataBound(object sender, GridItemEventArgs e)
        {
        }

        protected void ltncheck_Click(object sender, EventArgs e)
        {
            new StockBll().UpdatePOcheckStatus(POID, "1", base.CurrentUserEntity.UserName, DateTime.Now);
            ltnCancelcheck.Visible = true;
            ltncheck.Visible = false;
            link_saveBill.Visible = false;
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "window.location.reload();", true);
        }

        protected void ltnCancelcheck_Click(object sender, EventArgs e)
        {
            new StockBll().UpdatePOcheckStatus(POID,"0",base.CurrentUserEntity.UserName,DateTime.Now);
            ltnCancelcheck.Visible = false;
            ltncheck.Visible = true;
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "window.location.reload();", true);
        }
    }
}
